package com.example.membership.service.impl;
import com.example.membership.dao.UserDao;
import com.example.membership.model.User;
import com.example.membership.service.UserService;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    private final UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User getUserById(int id) {
        return userDao.findById(id);
    }

    @Override
    public List<User> getAllUsers() {
        return userDao.findAll();
    }

    @Override
    public void createUser(User user) {
        // 直接存储明文密码
        userDao.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userDao.update(user);
    }

    @Override
    public void deleteUser(int id) {
        userDao.delete(id);
    }


    @Override
    public User findByUsernameAndPassword(String username, String password) {
        try {
            return userDao.findByUsernameAndPassword(username, password);
        } catch (EmptyResultDataAccessException e) {
            throw new RuntimeException("用户名或密码错误");
        }
    }
}